#include <iostream>
#include <fstream>
#include "Logger.hpp"

Logger::Logger()
{
	// create/clean log.txt
	std::ofstream file("log.txt", std::ios::out | std::ios::trunc);
	if(!file)
		std::cerr << "Unable to clean log.txt." << std::endl;
	else
	{
		file.close();
		std::cout << "File log.txt cleaned." << std::endl;
	}
}
Logger::~Logger()
{
}

Logger& Logger::instance()
{
	static Logger l;
	return l;
}
		
Logger& Logger::writeInformation()
{
	std::cout << "INFO: ";
	
	std::ofstream file("log.txt", std::ios::app);
	if(!file)
		std::cerr << "Unable to write information in file log.txt." << std::endl;
	else
	{
		file << "INFO: ";
		file.close();
	}
	
	return *this;
}

Logger& Logger::writeWarning()
{
	std::cout << "WARNING: ";

	std::ofstream file("log.txt", std::ios::app);
	if(!file)
		std::cerr << "Unable to write warning in file log.txt." << std::endl;
	else
	{
		file << "WARNING: ";
		file.close();
	}
	
	return *this;
}

Logger& Logger::writeError()
{
	std::cout << "ERROR: ";

	std::ofstream file("log.txt", std::ios::app);
	if(!file)
		std::cerr << "Unable to write error in file log.txt." << std::endl;
	else
	{
		file << "ERROR: ";
		file.close();
	}
	
	return *this;
}		

Logger& Logger::operator<<(const std::string& s)
{
	std::cout << s;

	std::ofstream file("log.txt", std::ios::app);
	if(!file)
		std::cerr << "Unable to write string in file log.txt." << std::endl;
	else
	{
		file << s;
		file.close();
	}
	
	return *this;
}

Logger& Logger::operator<<(const float f)
{
	std::cout << f;

	std::ofstream file("log.txt", std::ios::app);
	if(!file)
		std::cerr << "Unable to write float in file log.txt." << std::endl;
	else
	{
		file << f;
		file.close();
	}

	return *this;
}

Logger& Logger::operator<<(const int i)
{
	std::cout << i;
	
	std::ofstream file("log.txt", std::ios::app);
	if(!file)
		std::cerr << "Unable to write integer in file log.txt." << std::endl;
	else
	{
		file << i;
		file.close();
	}

	return *this;
}


